<template>
    <el-dropdown :popper-class="popperClass" placement="bottom-end">
        <div class="nav-item pointer">
            <img class="user-img" :src="currentUser?.icon || config.defaultImg" alt="" />
            <span class="user-name" :title="username.length<=3? '': username">{{ shortName }}</span>
        </div>
        <template #dropdown>
            <div class="dropdown-menu-box">
                <div class="menu-header flex-between" @click="goSelfUrl('home.userHomePage')">
                    <div class="flex-start">
                        <img class="user-img" :src="currentUser?.icon || config.defaultImg" alt="" />
                        <span class="user-name" :title="username">{{ username }}</span>
                    </div>
                    <!-- <div class="personal-nav" v-if="showPersonCenter">
                        <span>个人中心</span>
                        <i class="iconfont icon-gerenzhongxin-you"></i>
                    </div> -->
                </div>
                <div class="menu-box" v-if="applications.length || showManage || showStrain">
                    <div class="application-menu" v-if="applications.length">
                        <div class="application-header">
                            我的应用
                        </div>
                        <ul class="application-list">
                            <li class="application-item flex-start" v-for="item in applications" :key="item.id" @click="pageJump(item.path, '_blank');">
                                <img class="application-icon" :src="item.icon" alt="" />{{item.shortTitle}}
                            </li>
                        </ul>
                    </div>
                    <div class="drop-menu-item flex-between" v-if="showStrain" @click="goSelfUrl('strain.index')">
                        <span>培训管理系统</span>
                        <i class="iconfont icon-gerenzhongxin-you"></i>
                    </div>
                    <div class="drop-menu-item flex-between" v-if="showManage" @click="goSelfUrl('idxmc.index')">
                        <span>管理中心</span>
                        <i class="iconfont icon-gerenzhongxin-you"></i>
                    </div>
                    <!-- <div class="drop-menu-item security flex-between" @click="goSelfUrl('home.accountSecurity')">
                        <span>账号安全</span>
                        <i class="iconfont icon-gerenzhongxin-you"></i>
                    </div> -->
                </div>
                <div class="logout flex-center" @click="exit">
                    <i class="iconfont icon-a-tuichu1x"></i>退出登录 
                </div>
            </div>
        </template>
    </el-dropdown>
</template>

<script setup>
import { pageJump } from '@/global/location-transfer';
import { getUrl } from '@/router/jumpHref.js';
import { ref, computed } from 'vue'
import { useStore } from 'vuex';
import { useRoute } from 'vue-router';
import config from '@/global/config'

const store = useStore();
const route = useRoute();

const props = defineProps({
    applications: { // 菜单
        type: Array,
        default: () => {
            return [];
        }
    },
    popperClass: { // 菜单
        type: String,
        default: () => {
            return 'user-drop-menu';
        }
    }
})
const currentUser = computed(() => {
    return store.state.currentUser || {};
})
const username = computed(() => {
    return currentUser.value?.realName || currentUser.value?.name || ''
})
const shortName = computed(() => {
    return username.value.length <= 3 ? username.value : (username.value.slice(0, 3) + '...')
})
const showManage = computed(() => { // 是否显示管理中心
    let pagePermission = store.state?.pagePermission || {};
    let isHaveIdmx = store.state.user?.isHaveIdmx;
    return !!pagePermission['manageCenter:base:home'] && isHaveIdmx;
})

const showPersonCenter = computed(() => { // 是否显示个人中心
    let pagePermission = store.state?.pagePermission || {};
    return !!pagePermission['personalCenter:page']
})

const showStrain = computed(() => { // 是否显示培训管理系统
    let pagePermission = store.state?.pagePermission || {};
    let isHaveStrain = store.state.user?.isHaveStrain;
    return !!pagePermission.continuingEducation && !!pagePermission.train && isHaveStrain;
})

function exit() {
   store.dispatch('logoutHome');
}

function goPage (name) {
    if(name === 'userHomePage' && !showPersonCenter.value) {
        return
    }
    pageJump({ name });
}

function goSelfUrl (str) {
    let url = getUrl(str);
    pageJump(url, '_blank');
}
</script>
<style lang="scss">
// .main-drop-menu {
//     .el-dropdown-menu__item {
//         justify-content: center;
//     }
// }
.user-drop-menu {
    width: 294px !important;
    background: #FFFFFF !important;
    box-shadow: 0px 0px 6px 0px rgba(0,0,0,0.1) !important;
    border-radius: 12px !important;
    .el-popper__arrow::before {
        display: none;
    }
}
</style>
<style lang="scss" scoped>

.pointer {
    cursor: pointer;
}
.user-img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
}
.user-name {
    margin-left: 6px;
    font-size: 14px;
    color: #000000;
}
.nav-item {
    display: flex;
    align-items: center;
    margin-left: 20px;
    &::after {
        content: '';
        background: url(~@/assets/img/login/head-down.png) no-repeat;
        width: 10px;
        height: 10px;
        background-size: contain;
        margin-left: 6px;
        display: inline-block;
    } 
    &:focus {
        outline: none;
    }
}
.dropdown-menu-box {
    .user-img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }
    .user-name {
        display: inline-block;
        max-width: 200px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        margin-left: 8px;
        font-size: 16px;
        color: #000000;
        line-height: 16px;
    }
    .menu-header {
        height: 64px;
        padding: 12px;
        cursor: pointer;
        &:hover {
            .personal-nav {
                color: var(--color-primary);
                i {
                    color: var(--color-primary);
                }
            }
        }
        .personal-nav {
            font-size: 14px;
            color: #222222;
            line-height: 14px;
            cursor: pointer;
            i {
                color: #999999;
                font-size: 12px;
                line-height: 12px;
                margin-left: 6px;
            }
            &:hover {
                color: var(--color-primary);
                i {
                    color: var(--color-primary);
                }
            }
        }
    }
    .menu-box {
        padding: 0 12px;
        // border-bottom: 1px solid #E0E2E4;
    }
    .application-menu {
        background: linear-gradient(135deg, #F9FDFA 0%, #F6FCF8 100%);
        border-radius: 8px;
        border: 1px solid #E5F6ED;
        .application-header {
            padding: 12px 16px;
            font-size: 14px;
            color: #666666;
            line-height: 14px;
        }
    }
    .menu-icon {
        width: 26px;
        height: 26px;
    }
    .menu-title {
        height: 16px;
        font-size: 16px;
        color: #222222;
        line-height: 16px;
    }
    .application-icon {
        width: 16px;
        height: 16px;
        margin-right: 8px;
    }
    .application-title {
        height: 16px;
        font-size: 16px;
        color: #222222;
        line-height: 16px;
        margin-left: 8px;
    }
    .application-list {
        display: flex;
        flex-wrap: wrap;
        padding: 0 0 10px;
        .application-item {
            width: 50%;
            height: 28px;
            font-size: 16px;
            color: #222222;
            line-height: 16px;
            padding-left: 16px;
            margin-top: 8px;
            align-self: center;
            cursor: pointer;
            &:hover {
                color: #279731;
            }
        }
        .application-item:nth-child(1), .application-item:nth-child(2) {
            margin-top: 0;
        }
        // .application-item:nth-child(2n) {
        //     margin-left: 20px;
        // }
    }
    .drop-menu-item {
        height: 44px;
        background: linear-gradient(135deg, #F9FDFA 0%, #F6FCF8 100%);
        border-radius: 8px;
        border: 1px solid #E5F6ED;
        margin-top: 12px;
        padding-left: 16px;
        padding-right: 14px;
        font-size: 14px;
        color: #222222;
        cursor: pointer;
        &:nth-child(1) {
            margin-top: 0;
        }
        i {
            font-size: 12px;
            line-height: 12px;
            color: #999999;
        }
        &:hover {
            color: #279731;
            i {
                color: #279731;
            }
        }
    }
    .logout {
        height: 48px;
        font-size: 14px;
        color: #666666;
        line-height: 14px;
        cursor: pointer;
        i {
            font-size: 16px;
            line-height: 16px;
            margin-right: 8px;
            color: #999999;
        }
        &:hover {
            color: var(--color-primary);
            i {
                color: var(--color-primary);
            }
        }
    }
}
</style>